from django.db import models
from tools.random_str import my_goods_id
from django.utils.safestring import mark_safe


# Create your models here.

# 商品一级分类
class GoodsCategory1(models.Model):
    name = models.CharField(max_length=20, unique=True, verbose_name='一级分类')
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '一级分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 商品二级级分类
class GoodsCategory2(models.Model):
    category1 = models.ForeignKey(GoodsCategory1, on_delete=models.CASCADE, verbose_name='一级分类')
    name = models.CharField(max_length=20, unique=True, verbose_name='二级分类')
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '二级分类'
        verbose_name_plural = verbose_name

    def show_category1(self):
        return self.category1.name

    show_category1.short_description = "一级分类"

    def __str__(self):
        return self.name


class GoodsPcbLib(models.Model):
    name = models.CharField(max_length=25, unique=True, verbose_name='封装')
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')
    desc = models.CharField(max_length=200, verbose_name='描述', null=True, blank=True)

    class Meta:
        verbose_name = '封装'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 商品SPU模型
class Goods(models.Model):
    # 一个元件只展示一张图片
    my_id = models.CharField(max_length=10, default=my_goods_id, primary_key=True, verbose_name='元件编号')
    image = models.ImageField(upload_to='img/goods/%Y/%m', default="img/goods/06.jpg", blank=True,
                              verbose_name="图片")
    name = models.CharField(max_length=25, verbose_name='元件名称')
    pcb = models.ForeignKey(GoodsPcbLib, on_delete=models.CASCADE, verbose_name='封装')
    stock = models.IntegerField(default=0, verbose_name='库存')
    category2 = models.ForeignKey(GoodsCategory2, on_delete=models.CASCADE, verbose_name='二级分类')
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')
    desc = models.CharField(max_length=200, verbose_name='描述', null=True, blank=True)

    class Meta:
        verbose_name = '元件'
        verbose_name_plural = verbose_name

    def show_image(self):
        return mark_safe("<img src='{}' height='50' width='50'/>".format(self.image.url))

    show_image.short_description = "图片"

    def __str__(self):
        return self.my_id


class GoodsSpecifications(models.Model):
    name = models.CharField(max_length=20, unique=True, verbose_name='规格名称')
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '规格'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsSpecificationsOption(models.Model):
    value = models.CharField(max_length=20, unique=True, verbose_name='规格选项')
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '规格选项'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.value


class GoodsSpecificationsInfo(models.Model):
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='元件')
    spec = models.ForeignKey(GoodsSpecifications, on_delete=models.CASCADE, verbose_name='规格名称')
    option = models.ForeignKey(GoodsSpecificationsOption, on_delete=models.CASCADE, verbose_name='规格选项')

    class Meta:
        verbose_name = '规格信息'
        verbose_name_plural = verbose_name

    def get_category2(self):
        return self.goods.category2.name

    get_category2.short_description = "二级分类"

    def __str__(self):
        return self.spec.name


# 元件品牌不是供应商
class GoodsBrand(models.Model):
    name = models.CharField(max_length=20, unique=True, verbose_name='品牌')
    image = models.ImageField(upload_to='img/brand/%Y/%m', default='img/brand/lc.png', blank=True,
                              verbose_name="logo")
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')
    desc = models.CharField(max_length=200, verbose_name='描述', null=True, blank=True)

    class Meta:
        verbose_name = '品牌'
        verbose_name_plural = verbose_name

    def show_image(self):
        return mark_safe("<img src='{}' height='45' width='160'/>".format(self.image.url))

    show_image.short_description = "logo"

    def __str__(self):
        return self.name


class GoodsMarket(models.Model):
    name = models.CharField(max_length=20, unique=True, verbose_name='商城')
    image = models.ImageField(upload_to="img/goodsmarket/%Y/%m", default='img/brand/lc.png', blank=True,
                              verbose_name="logo", max_length=100)
    telephone = models.CharField(max_length=20, null=True, blank=True, verbose_name='联系方式')
    address = models.CharField(max_length=50, verbose_name='地址', null=True, blank=True)
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')
    desc = models.CharField(max_length=100, verbose_name='描述', null=True, blank=True)

    class Meta:
        verbose_name = '商城'
        verbose_name_plural = verbose_name

    def show_image(self):
        return mark_safe("<img src='{}' height='50' width='160'/>".format(self.image.url))

    show_image.short_description = "商城logo"

    def __str__(self):
        return self.name


class GoodsMarketId(models.Model):
    goods = models.ForeignKey(Goods, to_field='my_id', on_delete=models.CASCADE, verbose_name='元件编号')
    market = models.ForeignKey(GoodsMarket, on_delete=models.CASCADE, verbose_name='元件商城')
    market_num = models.CharField(max_length=20, primary_key=True, verbose_name='商城编号')
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '编号'
        verbose_name_plural = verbose_name

    def get_goods_image(self):
        return self.goods.show_image()

    def show_market_image(self):
        return self.market.show_image()

    show_market_image.short_description = "商城"
    get_goods_image.short_description = "元件图片"

    def __str__(self):
        return self.market_num

# class GoodsBrandId(models.Model):
#     goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='元件编号')
#     brand = models.ForeignKey(GoodsBrand, on_delete=models.CASCADE, verbose_name='元件品牌')
#
#     date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')
#
#     class Meta:
#         verbose_name = '型号'
#         verbose_name_plural = verbose_name
#
#     def get_goods_image(self):
#         return self.goods.show_image()
#
#     def show_brand_image(self):
#         return self.brand.show_image()
#
#     show_brand_image.short_description = "品牌"
#     get_goods_image.short_description = "元件"
#
#     def __str__(self):
#         return self.brand_num
